﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Goods_Index.aspx.cs" Inherits="ncp.Web.cms.Goods_Index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="../../themes/icon.css" />
	<script type="text/javascript" src="../../js/jquery-1.7.1.min.js"></script>
	<script type="text/javascript" src="../../js/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../../js/common.js"></script>
</head>
<body style="margin:0; padding:0px;">
<form id="form1" runat="server">
    <div id="rightlayout" class="easyui-layout"  style="width:100%;">
	    <div id="west" region="west" split="true" title="商品类别" style="width:250px;padding:1px;overflow:auto;">
	        <div class="toolbar" style="background-color:#efefef">
		        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-add" onclick="onAdd();">增加子类</a>
		        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-remove" onclick="onDelete();">删除</a>
		        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-edit" onclick="onEdit();">编辑</a>
	        </div>
            <div id="divleft"></div>
	    </div>
	    <div  id="east"  region="center" title="商品资料">
	        <div class="toolbar" style="background-color:#efefef">
		        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-add" onclick="onAddGoods();">增加</a>
		        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-remove" onclick="onDeleteGoods();">删除</a>
		        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-edit" onclick="onEditGoods();">编辑</a>&nbsp;
                <span>查询条件:</span>
                <select id="ddlQuery">
                    <option value="G_Name">商品名称</option>
                    <option value="G_Code">商品编号</option>
                    <option value="GC_Name">商品类别</option>
                    <option value="G_Flag">发布位置</option>
                    <option value="G_StatusName">状态</option>
                </select>
                <select name="ddlOperator" id="ddlOperator">
	                <option value="like">包含</option>
	                <option value="=">等于</option>
                </select>
                <input name="txtQuery" type="text" id="txtQuery" style="width:80px;" />
    	        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-search" onclick="onSearch()">查询</a>
	        </div>
            <div id="divright">    
            </div>
	    </div>
	    <div id="divGoodsClassBox" class="easyui-dialog">
	        <p><span style="padding:10px;">商品类名：</span><input id="txtGC_Name" type="text" maxlength="10" /></p>
            <p id="GCflag"><span style="padding:10px;">自定义属性：</span>
	            <input type="checkbox" name="gc_flags" id="flagsh" value="h"/>首页[h]
	            <input  type="checkbox" name="gc_flags" id="flagsc" value="c"/>推荐[c]
            </p>
            <p style="text-align:center;">
	            <a href="#" class="easyui-linkbutton" onclick="onSave();">确定</a>&nbsp;&nbsp;
	            <a href="#" class="easyui-linkbutton" onclick="onClose();">关闭</a>
	        </p>
	    </div>
	    <div id="divGoodsInfoBox" class="easyui-dialog">
	        <p><span style="padding:10px;">商品编码：</span><input id="txtG_Code" type="text" maxlength="10" /></p>
	        <p><span style="padding:10px;">商品名称：</span><input id="txtG_Name" type="text" maxlength="25" /></p>
            <p id="flag"><span style="padding:10px;">自定义属性:</span>
    	        <input type="checkbox" name="flags" id="g_flagsh" value="h"/>首页[h]
<%--    	        <input  type="checkbox" name="flags" id="flagsc" value="c"/>推荐[c]
    	        <input  type="checkbox" name="flags" id="flagsa" value="a"/>特荐[a]   --%>       
    	    </p>
	        <p><span style="padding:10px;">状态：</span>
	            <input id="radioG_Status1" name="radioG_Status" type="radio" value="1" checked="checked" />启用
	            <input id="radioG_Status2" name="radioG_Status" type="radio" value="0" />禁用
	        </p>
	        <p style="text-align:center;">
	            <a href="#" class="easyui-linkbutton" onclick="onSaveGoods();">确定</a>&nbsp;&nbsp;
	            <a href="#" class="easyui-linkbutton" onclick="onCloseGoods();">关闭</a>
	        </p>
	    </div>
	</div>
	</form>
</body>
</html>
<script type="text/javascript">
    $(function() { //初始化
        $("#rightlayout").css("height", $(window).height() + "px");
        $("#west,#east").css("height", ($(window).height() - 35) + "px");
        LoadGoodsClass();
        initDialog();
    });


    //初始化对话框
    function initDialog() {
        $('#divGoodsClassBox').dialog({
            title: '商品类别',
            modal: true,
            width: 300,
            height: 200,
            left: 100,
            top: 150,
            padding: 10,
            closed:true
        });
        $('#divGoodsInfoBox').dialog({
            title: '商品资料',
            modal: true,
            width: 400,
            height: 250,
            left: 300,
            top: 150,
            padding: 10,
            closed: true
        });
    }
    
    //装载商品类别视图
    function LoadGoodsClass() {
        var response = ncp.Web.cms.Goods_Index.ShowGoodsClassTree();
        if (response.value != null) {
            var jsonOBJ = eval(response.value)[0];
            if (jsonOBJ.isreload) {
                $("#divleft").html(jsonOBJ.msg);
            } else
                alert(jsonOBJ.msg);
        }
    }

    //增加商品类别子类
    function onAdd() {
        var node = $('#tt1').tree('getSelected');
        if (node == null) {
            alert("请先选择一个节点！");
            return false;
        }
        $("#txtGC_Name").val("");
        $("#txtGC_Name").attr("gc_id", node.id);
        $("#txtGC_Name").attr("gc_level", $(node.text).attr("gc_level") / 1 + 1);
        $("#txtGC_Name").attr("gc_flag", $(node.text).attr("gc_flag"));
        $("#txtGC_Name").attr("cmd", "Add");
        if ($("#txtGC_Name").attr("gc_level") / 1 > 3) {
            alert("最多只能添加三级类别");
            return false;
        }
        $('#divGoodsClassBox').dialog('setTitle', '增加商品子类');
        $('#divGoodsClassBox').dialog("open");
        return false;
    }

    //编辑商品类别
    function onEdit() {
        var node = $('#tt1').tree('getSelected');
        if (node == null) {
            alert("请先选择一个节点！");
            return false;
        }
        if (node.id == 1 || node.id == 2) {
            alert("该不能编辑！");
            return false;
        }
        $('#divGoodsClassBox').dialog('setTitle', '编辑商品类别');
        $('#divGoodsClassBox').dialog("open");
        $("#txtGC_Name").val($(node.text).text());
        $("#txtGC_Name").attr("gc_id", node.id);
        $("#txtGC_Name").attr("gc_level", $(node.text).attr("gc_level"));
        $("#txtGC_Name").attr("gc_flag", $(node.text).attr("gc_flag"));
        $("#txtGC_Name").attr("cmd", "Edit");
        SetFlag($(node.text).attr("gc_flag"));
    }

    function SetFlag(gc_flag) {
        $("#GCflag input").removeAttr("checked");
        if (gc_flag != null) {
            if (gc_flag.indexOf("h") != -1) {
                $("#flagsh").attr("checked", true);
            }
            if (gc_flag.indexOf("c") != -1) {
                $("#flagsc").attr("checked", true);
            }
        }
        return false;
    }
    
    //删除商品类别
    function onDelete() {
        var node = $('#tt1').tree('getSelected');
        if (node == null) {
            alert("请先选择一个节点！");
            return false;
        }
        if (node.id == 1 || node.id == 2) {
            alert("该不能删除!");
            return false;
        }
        var b = $('#tt1').tree('isLeaf', node.target);
        if (!b) {
            alert("正面还有子节点，不能删除！");
            return false;
        }
        if (confirm("您确定要删除吗？")) {
            var response = ncp.Web.cms.Goods_Index.DeleteGoodsClass(node.id);
            if (response.value != null) {
                var jsonOBJ = eval(response.value)[0];
                if (jsonOBJ.isreload) {
                    $('#tt1').tree('remove', node.target);
                }
                alert(jsonOBJ.msg);
            }
        }
        return false;
    }
    
    //保存商品类别
    function onSave() {
        var obj =new Object();
        obj.cmd = $("#txtGC_Name").attr("cmd");
        obj.gc_id = $("#txtGC_Name").attr("gc_id");
        obj.gc_level = $("#txtGC_Name").attr("gc_level");
        obj.gc_flag = getGCFlag(); //$("#txtGC_Name").attr("gc_flag");
        obj.gc_name = $("#txtGC_Name").val();
        if (obj.gc_name == "") {
            alert("商品类别不能为空！");
            return false;
        }
        var jsonStr = $.objectToJson(obj);
        var response = ncp.Web.cms.Goods_Index.SaveGoodsClass(jsonStr);
        if (response.value != null) {
            var jsonOBJ = eval(response.value)[0];
            if (jsonOBJ.isreload) {
                $('#divGoodsClassBox').dialog("close");
                if (obj.cmd == "Add") {
                    obj.gc_id=jsonOBJ.gc_id;
                    append(obj);
                }
                else
                    update(obj);
            }
            else
                alert(jsonOBJ.msg);
        }
        return false;
    }

    //关闭商品类别对话框
    function onClose() {
        $('#divGoodsClassBox').dialog("close");
    }
    
    //追加一条商品类别
    function append(obj) {
        var node = $('#tt1').tree('getSelected');
        $('#tt1').tree('append', {
            parent: (node ? node.target : null),
            data: [{
                id: obj.gc_id,
                text: "<a gc_level=\"" + obj.gc_level + "\" gc_flag=\"" + obj.gc_flag + "\">" + obj.gc_name + "</a>",
                iconCls: (obj.gc_flag.indexOf("h") >= 0 ? "icon-ok" : "")
            }]
        })
    }

    //更新一条商品类别
    function update(obj) {
        var node = $('#tt1').tree('getSelected');
		if (node) {
		    node.text = "<a gc_level=\"" + obj.gc_level + "\" gc_flag=\"" + obj.gc_flag + "\">" + obj.gc_name + "</a>";
		    node.iconCls = (obj.gc_flag.indexOf("h") >= 0 ? "icon-ok" : "");
			$('#tt1').tree('update', node);
		}
    }

    //获取选中的flag
    function getGCFlag() {
        var flags = "";
        $("#GCflag input").each(function() {
            if ($(this).attr("checked")) {
                flags += "," + $(this).val();
            }
        });
        if (flags != "") {
            flags = flags.substring(1);
        }
        return flags;
    }
    
    //得到本节点和它的子节点
    function getChildren() {
        var node = $('#tt1').tree('getSelected');
        if (node == null) return "";

        var s = node.id;
        var children = $('#tt1').tree('getChildren', node.target);

        for (var i = 0; i < children.length; i++) {
            s +=','+  children[i].id ;
        }
        return s;
    }

</script>

<%--商品信息--%>
<script type="text/javascript">
    var pageobj = new Object();
    $(function() {
        initTree();
        initPageObj();
        initDatagrid();
        LoadGoodsInfo();
        initPager();
    });

    //初始化树
    function initTree() {
        $('#tt1').tree({
            onClick: function(node) {
                onSearch();
            }
        });
    }    
    
    //初始化页码对象
    function initPageObj() {
        pageobj.currentpageindex = 1;
        pageobj.pagesize = 10;
        pageobj.sorting = "G_Code";
        pageobj.sortdir = "ASC";
        pageobj.currentwhere = GetstrWhere();
        pageobj.recordcount = GetRecordCount();
    }

    //初始化表格
    function initDatagrid() {
        $('#divright').datagrid({
            height: $(window).height() - 61,
            nowrap: true,
            striped: true,
            singleSelect: true,
            sortName: 'G_Code',
            sortOrder: 'asc',
            remoteSort: false,
            idField: 'G_Id',
            pagination: true,
            rownumbers: true,
            columns: [[
					{ field: 'G_Id', title: '商品ID', hidden: 'true', width: 80 },
					{ field: 'G_Code', title: '商品编号', sortable: 'true', width: 120 },
					{ field: 'G_Name', title: '商品名称', sortable: 'true', width: 250 },
					{ field: 'GC_Name', title: '商品类别', sortable: 'true', width: 80, align: 'center' },
					{ field: 'G_Flag', title: '发布位置', sortable: 'true', width: 80, align: 'center',
					    formatter: function(value, rec) {
					        var strReturn = "";
					        if (value == null) return strReturn; ;
					        if (value.indexOf('h') >= 0) {
					            strReturn += "首页 ";
					        }
					        return strReturn;
					    }
					},
					{ field: 'G_StatusName', title: '状态', sortable: 'true', width: 60, align: 'center',
					    styler: function(value, row, index) {
					        if (value == '禁用') {
					            return 'background-color:#6293BB;color:#fff;font-weight:bold;';
					        }
					    }
					}
				]]
	    });
    }

    function initPager() {
        var p = $('#divright').datagrid('getPager');
        $(p).pagination({
            total: pageobj.recordcount,
            pageSize: pageobj.pagesize,
            pageList: [10, 15, 20, 25, 30],
            showRefresh: true,
            showPageList:true,
            beforePageText: '第',
            afterPageText: '页 共 {pages} 页',
            displayMsg: '当前显示 {from} - {to} 条记录 共 {total} 条记录',
            onSelectPage: function(pageNumber, pageSize) {
                $(this).pagination('loading');
                onShowPageNo(pageNumber, pageSize);
                $(this).pagination('loaded');
            },
            onBeforeRefresh: function(pageNumber, pageSize) {
                $(this).pagination('loading');
                onRefreshPageNo(pageNumber, pageSize);
                $(this).pagination('loaded');
            }
        });
    }
    
    //翻页
    function onShowPageNo(pageNumber, pageSize) {
        pageobj.pagesize = pageSize;
        pageobj.currentpageindex = pageNumber;
        LoadGoodsInfo();
        initPager();
        return false;
    }

    //刷新
    function onRefreshPageNo(pageNumber, pageSize) {
        initPageObj();
        return false;
    }
    
    //得到条件
    function GetstrWhere() {
        var myWhere = "1=1";
        var gcId = getChildren();
        if (gcId != "") myWhere += " and GC_Id in (" + gcId + ")";
        var response = ncp.Web.cms.Goods_Index.GetstrWhere($("#ddlQuery").val(), $("#ddlOperator").val(), $.trim($("#txtQuery").val()));
        if (response.value != null) {
            return myWhere + " and "+ response.value;
        }
        else
            return myWhere;
    }

    //得到总记录数
    function GetRecordCount() {
        var response = ncp.Web.cms.Goods_Index.GetRecordCount(pageobj.currentwhere);
        if (response.value != null) {
            return response.value;
        }
        else
            return 0;
    }

    //查询
    function onSearch() {
        pageobj.currentpageindex = 1;
        pageobj.currentwhere = GetstrWhere();
        pageobj.recordcount = GetRecordCount();
        LoadGoodsInfo();
        initPager();
        return false;
    }

    //装载商品信息视图
    function LoadGoodsInfo() {
        var jsonStr = $.objectToJson(pageobj);
        var data = eval(ncp.Web.cms.Goods_Index.BindTable(jsonStr).value);
        $('#divright').datagrid('loadData', data);
    }

    //增加商品
    function onAddGoods() {
        var node = $('#tt1').tree('getSelected');
        if (node == null) {
            alert("请先在左边树里选择一个商品的所属类别！");
            return false;
        }
        $('#divGoodsInfoBox').dialog('setTitle', '增加商品');
        $('#divGoodsInfoBox').dialog("open");

        $("#txtG_Code").val("");
        $("#txtG_Code").attr("gc_id", node.id);
        $("#txtG_Code").attr("gc_name", node.text);
        $("#txtG_Code").attr("cmd", "Add");
        $("#txtG_Name").val("");
        return false;
    }

    //编辑商品
    function onEditGoods() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            $('#divGoodsInfoBox').dialog('setTitle', '编辑商品');
            $('#divGoodsInfoBox').dialog("open");
            $("#txtG_Code").val(selected.G_Code);
            $("#txtG_Code").attr("g_id", selected.G_Id);
            $("#txtG_Code").attr("gc_id", selected.GC_Id);
            $("#txtG_Code").attr("gc_name", selected.GC_Name);
            $("#txtG_Code").attr("cmd", "Edit");
            $("#txtG_Name").val(selected.G_Name);
            $("input[name='radioG_Status'][value='" + selected.G_Status + "']").attr("checked", "true");
            SetGoodsFlag(selected.G_Flag);
        }
        else
            alert("请先选择一条记录！");
        return false;
    }

    //删除商品
    function onDeleteGoods() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            if (confirm("您确定要删除吗？")) {
                var response = ncp.Web.cms.Goods_Index.DeleteGoodsInfo(selected.G_Id);
                if (response.value != null) {
                    var jsonOBJ = eval(response.value)[0];
                    if (jsonOBJ.isreload) {
                        var index = $('#divright').datagrid('getRowIndex', selected);
                        $('#divright').datagrid('deleteRow', index);
                    }
                    alert(jsonOBJ.msg);
                }
            } 
        }
        else
            alert("请先选择一条记录！");
        return false;
    }

    //保存商品信息
    function onSaveGoods() {
        var obj = new Object();
        obj.cmd = $("#txtG_Code").attr("cmd");
        obj.g_id = $("#txtG_Code").attr("g_id");
        obj.gc_id = $("#txtG_Code").attr("gc_id");
        obj.gc_name = $("#txtG_Code").attr("gc_name");
        obj.g_code = $("#txtG_Code").val();
        obj.g_name = $("#txtG_Name").val();
        obj.g_flag = getFlag();
        obj.g_status = $("input[name='radioG_Status']:checked").attr("value");
        if (obj.g_code == "") {
            alert("商品编码不能为空！");
            return false;
        }
        if (obj.g_name == "") {
            alert("商品名称不能为空！");
            return false;
        }
        var jsonStr = $.objectToJson(obj);
        var response = ncp.Web.cms.Goods_Index.SaveGoodsInfo(jsonStr);
        if (response.value != null) {
            var jsonOBJ = eval(response.value)[0];
            if (jsonOBJ.isreload) {
                $('#divGoodsInfoBox').dialog("close");
                if (obj.cmd == "Add")
                    appendRow(jsonOBJ.g_id, obj);
                else 
                    updateRow(obj);
            }
            else
                alert(jsonOBJ.msg);
        }
        return false;
    }

    //关闭商品信息对话框
    function onCloseGoods() {
        $('#divGoodsInfoBox').dialog("close");
    }

    function SetGoodsFlag(g_flag) {
        $("#flag input").removeAttr("checked");
        if (g_flag != null) {
            if (g_flag.indexOf("h") != -1) {
                $("#g_flagsh").attr("checked", true);
            }
            if (g_flag.indexOf("c") != -1) {
                $("#g_flagsc").attr("checked", true);
            }
        }
        return false;
    }
    
    //获取选中的flag
    function getFlag() {
        var flags = "";
        $("#flag input").each(function() {
            if ($(this).attr("checked")) {
                flags += "," + $(this).val();
            }
        });
        if (flags != "") {
            flags = flags.substring(1);
        }
        return flags;
    }
    
    //选择多条商品记录
    function getSelections() {
        var ids = [];
        var rows = $('#divright').datagrid('getSelections');
        for (var i = 0; i < rows.length; i++) {
            ids.push(rows[i].code);
        }
        alert(ids.join(':'));
    }

    //增加一条商品记录
    function appendRow(g_id,obj) {
        $('#divright').datagrid('appendRow', {
            G_Id: g_id,
            G_Code: obj.g_code,
            G_Name: obj.g_name,
            GC_Name: obj.gc_name,
            G_Flag : obj.g_flag,
            G_StatusName: (obj.g_status=="1"?"启用":"禁用")
        });
    }

    //修改一条商品记录
    function updateRow(obj) {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            var index = $('#divright').datagrid('getRowIndex', selected);
            selected.G_Id = obj.g_id;
            selected.G_Code = obj.g_code;
            selected.G_Name = obj.g_name;
            selected.GC_Name = obj.gc_name;
            selected.G_Flag = obj.g_flag;
            selected.G_StatusName = (obj.g_status == "1" ? "启用" : "禁用");
            $('#divright').datagrid('refreshRow', index);
        }
    }
</script>
